<?php

namespace App\Admin\Controllers;

use App\Models\Course;
use App\Models\Traning;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class TraningController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '培训管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Traning());
        $grid->column('id', __('Id'));
        $grid->column('title', __('培训名称'));
        $grid->column('thumb', __('缩略图'));
        $grid->column('is_recommend', __('是否推荐'))->display(function ($is_recommend) {
            return $is_recommend ? '是' : '否';
        });
        $grid->column('is_audit', __('是否开启报名审核'));
        $grid->column('start_at', __('培训开始时间'));
        $grid->column('end_at', __('培训结束时间'));
        $grid->column('status', __('状态'));
        $grid->column('created_at', __('创建时间'));
        


        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Traning::findOrFail($id));
        $show->field('id', __('Id'));
        $show->field('name', __('培训名称'));
        $show->field('start_at', __('培训开始时间'));
        $show->field('end_at', __('培训结束时间'));
        $show->field('status', __('状态'))->using([0 => '正常', 1 => '禁用']);
        $show->field('is_audit', __('是否审核'))->using([0 => '不审核', 1 => '审核']);
        $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('更新时间'));
        $show->field('description', __('培训简介'));
        $show->field('content', __('培训内容'));
        $show->field('sort', __('排序'));
        $show->field('is_recommend', __('是否推荐'))->using([1 => '是', 0 => '否']);
        $show->field('thumb', __('缩略图'))->image();
        


        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Traning());
        $form->text('title', __('培训名称'));
        $form->image('thumb', __('缩略图'));
        $form->radio('is_recommend', __('是否推荐'))->options([1 => '是', 0 => '否']);
        $form->radio('status', __('状态'))->options([0 => '正常', 1 => '禁用']);
        $form->textarea('content');
        $form->number('sort', __('排序'))->default(99);
        $form->datetime('start_time', __('开始时间'));
        $form->datetime('end_time', __('结束时间'));
        $form->hasMany('traningCourseRelation', '课程', function (Form\NestedForm $form) {
           $form->select('course_id', __('课程'))->options(Course::all()->pluck('name', 'id'));
        });

        return $form;
    }
}
